using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class EntityBindPointComponent : TReusableEvtComponent
    {
        //模型的根节点
        public TransformAgent bindPointRoot { get; private set; }

        //保存所有的绑点的Transform
        private Dictionary<int, Transform> _bindPointDic;

        public override void OnCreate(TEntity entity)
        {
            base.OnCreate(entity);
            _bindPointDic = new Dictionary<int, Transform>();
        }

        public override void OnComponentEvent<T>(TComponentEvent eventType, T arg)
        {
            switch (eventType)
            {
                case TComponentEvent.OnEntityObjectLoaded:
                    bindPointRoot = _entity.transform;
                    break;
                case TComponentEvent.OnEntityObjectHided:
                    _bindPointDic.Clear();
                    break;
            }
        }

        public Transform GetBindPoint(EntityBindPointCommonType type)
        {
            return GetBindPoint((int)type);
        }

        public Transform GetBindPoint(int pointId)
        {
            //处理overide Point 逻辑
            if (_bindPointDic.TryGetValue(pointId, out var point))
            {
                return point;
            }
            point = BindPointUtil.GetBindPoint(pointId, bindPointRoot);
            _bindPointDic[pointId] = point;
            return point;
        }

        #region ObjectPool

        public override uint MaxStore => 50;

        public override void OnPoolReset()
        {
            _bindPointDic?.Clear();
            _bindPointDic = null;
            bindPointRoot = null;
        }

        #endregion
    }
}
